package cn.com.wangluotongxin.merchants.controller;

import cn.com.wangluotongxin.common.utils.TenantUtils;
import cn.com.wangluotongxin.common.web.AjaxResult;
import cn.com.wangluotongxin.merchants.eureka.form.AddMerchantsMenuEditForm;
import cn.com.wangluotongxin.merchants.eureka.form.AddMerchantsMenuForm;
import cn.com.wangluotongxin.merchants.eureka.form.InitPlatformMenuForm;
import cn.com.wangluotongxin.merchants.service.IMerchantsMenuService;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

@Api(tags = "网络通信- 菜单权限服务相关接口")
@RestController
@RequestMapping("/inner/menu")
public class InnerMenuController {

    @Autowired
    private IMerchantsMenuService menuService;

    @ApiOperation("商户资源菜单权限初始化")
    @RequestMapping("/init")
    public AjaxResult init(@RequestBody InitPlatformMenuForm form) {

        menuService.initMenu(TenantUtils.get().getKey().toString());
        return AjaxResult.success();
    }

    @ApiOperation("添加菜单")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody AddMerchantsMenuForm form) {
        Assert.isTrue(StrUtil.isNotBlank(form.getMenuName()), "缺少名称");
        Assert.isTrue(StrUtil.isNotBlank(form.getMenuValue()), "缺少权限值");

        menuService.add(form);
        return AjaxResult.success();
    }

    @ApiOperation("编辑菜单")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody AddMerchantsMenuEditForm form) {
        Assert.notNull(form.getId(), "缺少数据ID");
        Assert.isTrue(StrUtil.isNotBlank(form.getMenuName()), "缺少名称");
        Assert.isTrue(StrUtil.isNotBlank(form.getMenuValue()), "缺少权限值");

        menuService.edit(form);
        return AjaxResult.success();
    }

    @ApiOperation("删除")
    @PostMapping("/delete/{menuId}")
    public AjaxResult delete(@PathVariable("menuId") Integer menuId) {
        menuService.delete(menuId);
        return AjaxResult.success();
    }
}
